package http

import (
	"fmt"
	"net/http"
	"net/http/pprof"
	"strings"
	"time"
)

func AddHandleFunc() {
	http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("hello, " + request.URL.Path + "\n"))
		writer.Write([]byte("hello, " + request.URL.RequestURI() + "\n"))
		writer.WriteHeader(200)
	})

	http.HandleFunc("/hello1", func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("hello1, " + request.URL.Path + "\n"))
		writer.Write([]byte("hello1, " + request.URL.RequestURI() + "\n"))
		writer.WriteHeader(200)
	})
}

func TestServer1() {
	AddHandleFunc()
	fmt.Println("start http server")
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}

type MyHandler struct {
}

func (*MyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	path := request.URL.Path
	if strings.HasPrefix(path, "/hello") {
		writer.Write([]byte("hello, " + request.URL.Path + "\n"))
		writer.Write([]byte("hello, " + request.URL.RequestURI() + "\n"))

	} else if strings.HasPrefix(path, "/aHello") {
		request.ParseForm()
		request.ParseMultipartForm(32 << 20)

		fmt.Println(request.Form)

		time.Sleep(time.Second * 1)
		writer.Write([]byte("aHello, " + request.URL.Path + "\n"))
		writer.Write([]byte("aHello, " + request.URL.RequestURI() + "\n"))
	} else if strings.HasPrefix(path, "/debug/pprof/") {
		pprof.Index(writer, request)

	} else if strings.HasPrefix(path, "/debug/pprof/cmdline") {
		pprof.Cmdline(writer, request)
	} else if strings.HasPrefix(path, "/debug/pprof/profile") {
		pprof.Profile(writer, request)
	} else if strings.HasPrefix(path, "/debug/pprof/symbol") {
		pprof.Symbol(writer, request)
	} else if strings.HasPrefix(path, "/debug/pprof/trace") {
		pprof.Trace(writer, request)
	} else {
		writer.Write([]byte("not found, " + request.URL.Path + "\n"))
	}

	writer.WriteHeader(200)
}

func TestServer2() {
	err := http.ListenAndServe(":8080", &MyHandler{})
	if err != nil {
		fmt.Println(err.Error())
		return
	}

}

func TestServer3() {
	server := http.Server{
		Addr:           ":8080",
		Handler:        &MyHandler{},
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	err := server.ListenAndServe()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}

func TestHttpsServer() {
	err := http.ListenAndServeTLS(":8080",
		"/Users/fanshuai/cert/server.pem", //服务器证书
		"/Users/fanshuai/cert/server.key", //服务器私钥
		&MyHandler{})

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	/**
	生成证书步骤：
	1. 生成私钥
	openssl genrsa -out server.key 2048

	2. 创建自签名CA证书（本地创建自签名证书，浏览器不信任）
	openssl req -new -x509 -key server.key -out ca.pem -days 3650

	3. 创建服务器证书的申请文件server.csr
	openssl req -new -key server.key -out server.csr

	4. 通过自签名CA证书，创建服务器证书server.pem
	openssl x509 -req -days 3650 -in server.csr -CA ca.pem -CAkey server.key -CAcreateserial -out server.pem

	5. 查看证书：
	openssl x509 -in server.pem -noout -serial -subject   //-noout表示不生成证书
	openssl x509 -in server.pem -noout -text

	备注：CA服务器根据证书请求创建证书：
	openssl ca -in server.csr -out server.pem -days 3650
	*/
}
